package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.controller.BaseController;
import com.hoshiicloud.user.entity.organization.ComOrganizationUsers;
import com.hoshiicloud.user.param.organization.ComOrganizationUsersAddParam;
import com.hoshiicloud.user.param.organization.ComOrganizationUsersQueryParam;
import com.hoshiicloud.user.param.organization.ComOrganizationUsersUpdateParam;
import com.hoshiicloud.user.service.organization.ComOrganizationUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 租户机构用户 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-27
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/organization/com-organization-users")
@Api(value = "SAAS租户机构用户", tags = {"SAAS租户机构用户"})
@ApiSort(14)
public class ComOrganizationUsersController extends BaseController {
    @Autowired
    private ComOrganizationUsersService comOrganizationUsersService;

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 分页查询租户机构用户
    */
    @ApiOperation(value = "分页查询租户机构用户")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ComOrganizationUsersQueryParam param) {
        try {
            log.info("分页查询租户机构用户");
            QueryWrapper<ComOrganizationUsers> queryWrapper = new QueryWrapper<ComOrganizationUsers>();
            queryWrapper.eq(param.getMemberId() != null&&param.getMemberId() !=0, "member_id", param.getMemberId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getUserCode()), "user_code", param.getUserCode());
            queryWrapper.eq(StringUtils.isNotBlank(param.getQrCode()), "qr_code", param.getQrCode());
            queryWrapper.eq(StringUtils.isNotBlank(param.getWorkTelphone()), "work_telphone", param.getWorkTelphone());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPosition()), "position", param.getPosition());
            queryWrapper.eq(StringUtils.isNotBlank(param.getEducational()), "Educational", param.getEducational());
            queryWrapper.eq(StringUtils.isNotBlank(param.getProfessional()), "professional", param.getProfessional());
            queryWrapper.eq(param.getHireDate() != null, "hire_date", param.getHireDate());
            queryWrapper.eq(StringUtils.isNotBlank(param.getEmail()), "email", param.getEmail());
            queryWrapper.eq(StringUtils.isNotBlank(param.getOfficelocation()), "officelocation", param.getOfficelocation());
            queryWrapper.eq(param.getLocked() != null, "locked", param.getLocked());
            queryWrapper.eq(StringUtils.isNotBlank(param.getWxOpenId()), "wx_open_id", param.getWxOpenId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getWxUnionId()), "wx_union_id", param.getWxUnionId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getWxName()), "wx_name", param.getWxName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getWxPic()), "wx_pic", param.getWxPic());
            queryWrapper.eq(StringUtils.isNotBlank(param.getQqOpenId()), "qq_open_id", param.getQqOpenId());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            queryWrapper.eq(param.getLastLoginTime() != null, "last_login_time", param.getLastLoginTime());
            queryWrapper.select(BeanUtils.getSqlSelect(ComOrganizationUsers.class));
            Page<ComOrganizationUsers> page = new Page<ComOrganizationUsers>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(comOrganizationUsersService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询租户机构部门异常", e);
            return Response.failed("分页查询租户机构部门异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 根据id查询租户机构用户
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询租户机构用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(comOrganizationUsersService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增租户机构用户")
    public Response save(@RequestBody @Valid ComOrganizationUsersAddParam param) {
        ComOrganizationUsers data;
        try {
            data = BeanUtils.covertType(param, ComOrganizationUsers.class);
        } catch (Exception e) {
            log.error("新增租户机构Bean转换异常", e);
            return Response.failed("新增租户机构Bean转换异常");
        }
        return Response.data(comOrganizationUsersService.insert(data));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 修改租户机构用户
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改租户机构用户")
    public Response update(@RequestBody @Valid ComOrganizationUsersUpdateParam param) {
        ComOrganizationUsers data;
        try {
            data = BeanUtils.covertType(param, ComOrganizationUsers.class);
        }catch (Exception e) {
            log.error("修改租户机构Bean转换异常", e);
            return Response.failed("修改租户机构Bean转换异常");
        }
        return Response.data(comOrganizationUsersService.updateById(data));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除租户机构用户
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除租户机构用户")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        if(comOrganizationUsersService.deleteById(id)){
            return Response.data("删除租户机构用户成功");
        }else{
            return Response.data("删除租户机构用户失败");
        }
    }

    /**
    * @Author:campchen
    * @Description: StringUtils.join(ids, "") 为缓存中添加唯一KEY  如不合适可选择更好方法
    * @Date:上午 10:18 2019/5/11
    * @Param:  ids
    * @return: com.hoshiicloud.common.rpc.Response
    */
    @ApiOperation(value = "根据id集合查询租户机构用户")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if(CollectionUtils.isNotEmpty(ids)){
            ids.sort(null);
            return Response.data(comOrganizationUsersService.getByIds(ids,StringUtils.join(ids, "")));
        }else{
            return  Response.data(null);
        }
    }


    }
